﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "TowardsTarget", menuName = "BulletMotion/TowardsTarget(默认朝向玩家)")]
public class TowardsTarget : BulletMotion
{
    public float angularVelocity;
    public GameObject target;

    public TowardsTarget(float awake, float life, float a, GameObject t)
    {
        awakeTime = awake;
        lifeTime = life;
        angularVelocity = a;
        target = t;
    }

    public override Vector2 Motion(float time)
    {
        if (target == null)
        {
            //target = GameMgr.Get<IPlayerManager>().GetData().Player.gameObject;
            return Vector2.zero;
        }
        else
        {
            if (awake)
            {
                float rad, k = Vector2.SignedAngle(motionController.towards, target.transform.position - motionController.transform.position);
                if (k > 0)
                {
                    if (k - angularVelocity * Time.fixedDeltaTime > 0)
                    {
                        rad = angularVelocity * Time.fixedDeltaTime * Mathf.PI / 180;
                    }
                    else
                    {
                        rad = k * Mathf.PI / 180;
                    }
                }
                else if (k < 0)
                {
                    if (k + angularVelocity * Time.fixedDeltaTime < 0)
                    {
                        rad = -1 * angularVelocity * Time.fixedDeltaTime * Mathf.PI / 180;
                    }
                    else
                    {
                        rad = k * Mathf.PI / 180;
                    }
                }
                else
                {
                    rad = 0;
                }
                float x = Mathf.Cos(rad);
                float y = Mathf.Sin(rad);
                motionController.towards = new Vector2(Vector2.Dot(motionController.towards, new Vector2(x, -y)), Vector2.Dot(motionController.towards, new Vector2(y, x)));
            }
            return Vector2.zero;
        }
    }
}
